﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;

public partial class CRM_Store_ERP_DeliveryEdit : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int c = BindCenterStore(Store_Rome, Request.QueryString["ptabid"]);
            if (c > 0)
                extcomb_StoreRoomId.SelectedIndex = 0;
            string objid = ViewState_ObjID = string.IsNullOrEmpty(Request.QueryString["ObjId"]) ? "Add" : Request.QueryString["ObjId"];
            ShowEditInfo(objid);
        }
    }

    private void ShowEditInfo(string objid)
    {
        var planDataTable = CRMMis.BLL.ERP_OutBound.GetList("BoundId=" + objid).Tables[0];
        if (planDataTable.Rows.Count > 0)
        {
            DataRow row = planDataTable.Rows[0];
            int planState = Convert.ToInt32(row["State"]);
                
            SetContainerReadOnly(formPanel, true);
                
            SetContainer(formPanel, planDataTable);
            //单独绑定客户
            DataTable suppData = CRMMis.BLL.ERP_Customer.GetList("CustomerId=" + row["CustomerId"], "CustomerName,Spell,Address").Tables[0];
            Store_Customer.DataSource = suppData;
            Store_Customer.DataBind();

            var planDetail = CRMMis.BLL.ERP_OutBoundDetail.GetList(" DBoundId=" + objid).Tables[0];
            Store1.DataSource = planDetail;
            Store1.DataBind();
            extcomb_CustomerId.ReadOnly = extcomb_StoreRoomId.ReadOnly = planDetail.Rows.Count > 0;
        }
        
    }


    protected void OnExtBtn_ToExcel_Click(object sender, DirectEventArgs e)
    {
        var planDetail = CRMMis.BLL.ERP_OutBoundDetail.GetList(" DBoundId=" + ViewState_ObjID).Tables[0];
        string name = "送货单";
        CreateExcelOutput(name, name, planDetail, gridPanel_listdata, false, sheet1 =>
        {
            var row = sheet1.CreateRow(1);
            row.CreateCell(0).SetCellValue(exttxtf_DeliveryNo.FieldLabel + "：" + exttxtf_DeliveryNo.Text);
            sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 2));
            row.CreateCell(3).SetCellValue(exttxtf_InUserName.FieldLabel + "：" + exttxtf_InUserName.Text);
            sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 3, 5));
            row.CreateCell(6).SetCellValue(exttxtf_InDate.FieldLabel + "：" + exttxtf_InDate.Text);
            sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 6, 8));

            var row2 = sheet1.CreateRow(2);
            row2.CreateCell(0).SetCellValue(extcomb_StoreRoomId.FieldLabel + "：" + extcomb_StoreRoomId.SelectedItem.Text);
            sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(2, 2, 0, 2));
            row2.CreateCell(3).SetCellValue(extcomb_CustomerId.FieldLabel + "：" + extcomb_CustomerId.SelectedItem.Text);
            sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(2, 2, 3, 5));

            int[] widths = new int[] { 20, 20, 20, 20, 15, 15, 15, 15, 15 };
            for (int i = 0; i < widths.Length; i++)
            {
                sheet1.SetColumnWidth(i, widths[i] * 256);
            }
            return 2;
        });
    }




}